package com.jackstraw.jack_base.datasource.moredatasource.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.jackstraw.jack_base.datasource.database.parten.config.DataBaseConfiguration;
import com.jackstraw.jack_base.datasource.database.parten.entity.bean.DataBaseTemplateCut;
import com.jackstraw.jack_base.datasource.moredatasource.bean.DruidDataBaseEntity;
import com.jackstraw.jack_base.datasource.moredatasource.bean.DruidDataBaseFactory;
import com.jackstraw.jack_base.datasource.moredatasource.bean.DruidDataBaseProperties;
import com.jackstraw.jack_base.util.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;


@Slf4j
@Configuration
@EnableTransactionManagement
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class DruidDataBaseConfiguration
        extends DataBaseConfiguration<DruidDataBaseEntity, DruidDataBaseFactory, DruidDataBaseProperties, DruidDataBaseConfiguration> {


    public DruidDataBaseConfiguration(ApplicationContext applicationContext, Environment environment) {
        super(applicationContext, environment);
    }

    @Override
    public DruidDataBaseProperties properties() {
        return new DruidDataBaseProperties();
    }

    @Override
    public DruidDataBaseFactory dataSourceBuildFactory() {
        return new DruidDataBaseFactory();
    }

    private static final String DEFAULT_ALLOW_IP = "127.0.0.1";

    @Bean
    @ConditionalOnBean(name = {"databaseProperties"})
    @ConditionalOnProperty(prefix = "jack.datasource.database", name = "stat-view-servlet.enabled", havingValue = "true")
    public ServletRegistrationBean statViewServletRegistrationBean() {
        DruidDataBaseProperties properties = SpringUtil.getBean("databaseProperties", DruidDataBaseProperties.class);

        DruidDataBaseProperties.StatViewServlet config = properties.getStatViewServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean();
        registrationBean.setServlet(new StatViewServlet());
        registrationBean.addUrlMappings(config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*");
        if (config.getAllow() != null) {
            registrationBean.addInitParameter("allow", config.getAllow());
        } else {
            registrationBean.addInitParameter("allow", DEFAULT_ALLOW_IP);
        }
        if (config.getDeny() != null) {
            registrationBean.addInitParameter("deny", config.getDeny());
        }
        if (config.getLoginUsername() != null) {
            registrationBean.addInitParameter("loginUsername", config.getLoginUsername());
        }
        if (config.getLoginPassword() != null) {
            registrationBean.addInitParameter("loginPassword", config.getLoginPassword());
        }
        if (config.getResetEnable() != null) {
            registrationBean.addInitParameter("resetEnable", config.getResetEnable());
        }
        return registrationBean;
    }


    @Bean
    @ConditionalOnBean(name = {"databaseProperties"})
    @ConditionalOnProperty(prefix = "jack.datasource.database", name = "web-stat-filter.enabled", havingValue = "true")
    public FilterRegistrationBean webStatFilterRegistrationBean() {
        DruidDataBaseProperties properties = SpringUtil.getBean("databaseProperties", DruidDataBaseProperties.class);
        DruidDataBaseProperties.WebStatFilter config = properties.getWebStatFilter();
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        WebStatFilter filter = new WebStatFilter();
        registrationBean.setFilter(filter);
        registrationBean.addUrlPatterns(config.getUrlPattern() != null ? config.getUrlPattern() : "/*");
        registrationBean.addInitParameter("exclusions", config.getExclusions() != null ? config.getExclusions() : "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        if (config.getSessionStatEnable() != null) {
            registrationBean.addInitParameter("sessionStatEnable", config.getSessionStatEnable());
        }
        if (config.getSessionStatMaxCount() != null) {
            registrationBean.addInitParameter("sessionStatMaxCount", config.getSessionStatMaxCount());
        }
        if (config.getPrincipalSessionName() != null) {
            registrationBean.addInitParameter("principalSessionName", config.getPrincipalSessionName());
        }
        if (config.getPrincipalCookieName() != null) {
            registrationBean.addInitParameter("principalCookieName", config.getPrincipalCookieName());
        }
        if (config.getProfileEnable() != null) {
            registrationBean.addInitParameter("profileEnable", config.getProfileEnable());
        }
        return registrationBean;
    }

}